package com.oron3.locker;

import java.util.ArrayList;

import com.oron3.lock.R;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class ToolController {
	private final ViewGroup tool;
	private final CompoundButton edit;

	public interface ToolListener {
		public void onEditChecked(boolean b);

		public void onAddChecked(boolean b);

		public void onAddClicked();

		public void searchRequested(String query);

		public void onSave();

		public void onTitleEdited(String newer);
	}

	public static class ToolAdapter implements ToolListener {

		/*
		 * (non-Javadoc)
		 *
		 * @see
		 * com.oron3.locker.ToolController.ToolListener#onEditChecked(boolean)
		 */
		@Override
		public void onEditChecked(final boolean b) {
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see com.oron3.locker.ToolController.ToolListener#onAddClicked()
		 */
		@Override
		public void onAddClicked() {
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see
		 * com.oron3.locker.ToolController.ToolListener#searchRequested(java
		 * .lang.String)
		 */
		@Override
		public void searchRequested(final String query) {
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see
		 * com.oron3.locker.ToolController.ToolListener#onAddChecked(boolean)
		 */
		@Override
		public void onAddChecked(final boolean b) {
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see com.oron3.locker.ToolController.ToolListener#onSave()
		 */
		@Override
		public void onSave() {
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see
		 * com.oron3.locker.ToolController.ToolListener#onTitleEdited(java.lang
		 * .String)
		 */
		@Override
		public void onTitleEdited(final String newer) {
		}
	}

	private final ArrayList<ToolListener> listeners = new ArrayList<ToolListener>();
	private final ToggleButton addToggle;
	private final ImageButton add;
	private final ImageButton save;
	private final EditText editTitle;
	private final TextView title;
	private boolean titleVisible = false;

	public ToolController(final ViewGroup tool) {
		this.tool = tool;
		edit = (ToggleButton) tool.findViewById(R.id.edit);
		addToggle = (ToggleButton) tool.findViewById(R.id.addToggle);
		edit.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
				for (final ToolListener listener : listeners)
					listener.onEditChecked(isChecked);
				editTitle.setVisibility(titleVisible && isChecked ? View.VISIBLE : View.GONE);
				title.setVisibility(titleVisible && !isChecked ? View.VISIBLE : View.GONE);

			}
		});
		add = (ImageButton) tool.findViewById(R.id.add);
		save = (ImageButton) tool.findViewById(R.id.save);
		save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				for (final ToolListener listener : listeners)
					listener.onSave();
			}
		});
		editTitle = (EditText) tool.findViewById(R.id.title_edit);
		editTitle.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
				for (final ToolListener listener : listeners)
					listener.onTitleEdited(s.toString());
				title.setText(s.toString());
			}

			@Override
			public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
			}

			@Override
			public void afterTextChanged(final Editable s) {
			}
		});
		editTitle.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(final View v, final boolean hasFocus) {
				editTitle.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
				title.setVisibility(!hasFocus ? View.VISIBLE : View.GONE);

			}
		});
		title = (TextView) tool.findViewById(R.id.title);
		title.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				setEditState(true);
			}
		});
		setAddAsToggle(false);
		// final SearchView search =
		// (SearchView)tool.findViewById(R.id.searchView1);
		// search.setIconifiedByDefault(false);
		// search.setOnQueryTextListener(new OnQueryTextListener() {
		//
		// @Override
		// public boolean onQueryTextSubmit(final String query) {
		// editBroadcaster.broadcast(new Delegate<ToolListener>() {
		// @Override
		// public void invoke(final ToolListener t) {
		// t.searchRequested(query);
		// }
		// });
		// return true;
		// }
		//
		// @Override
		// public boolean onQueryTextChange(final String newText) {
		// return false;
		// }
		// });

	}

	public void setSaveVisibility(final boolean visible) {
		save.setVisibility(visible ? View.VISIBLE : View.GONE);
	}

	public void setAddAsToggle(final boolean toggle) {
		if (toggle) {
			addToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
					for (final ToolListener listener : listeners)
						listener.onAddChecked(isChecked);
				}
			});
			addToggle.setVisibility(View.VISIBLE);
			add.setVisibility(View.GONE);
		} else {
			add.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(final View v) {
					for (final ToolListener listener : listeners)
						listener.onAddClicked();

				}
			});
			add.setVisibility(View.VISIBLE);
			addToggle.setVisibility(View.GONE);
		}
	}

	public boolean getEditState() {
		return edit.isChecked();
	}

	public boolean getAddState() {
		return addToggle.isChecked();
	}

	public void setEditVisibility(final boolean visible) {
		edit.setVisibility(visible ? View.VISIBLE : View.GONE);
	}

	public void setTitleVisibility(final boolean visible) {
		titleVisible = visible;
		title.setVisibility(titleVisible && !edit.isChecked() ? View.VISIBLE : View.GONE);
		editTitle.setVisibility(titleVisible && edit.isChecked() ? View.VISIBLE : View.GONE);
	}

	public void setEditState(final boolean state) {
		edit.setChecked(state);
		editTitle.setVisibility(titleVisible && state ? View.VISIBLE : View.GONE);
		title.setVisibility(titleVisible && !state ? View.VISIBLE : View.GONE);
	}

	public boolean registerToolListener(final ToolListener listener) {
		return registerToolListener(listener, true);
	}

	public boolean registerToolListener(final ToolListener listener, final boolean invokeNow) {
		if (invokeNow) {
			listener.onEditChecked(getEditState());
			listener.onAddChecked(getAddState());
		}

		return listeners.contains(listener) ? false : listeners.add(listener);
	}

	public boolean unregisterToolListener(final ToolListener listener) {
		return listeners.remove(listener);
	}

	/**
	 * @param b
	 */
	public void setAddState(final boolean state) {
		addToggle.setChecked(state);
	}

	/**
	 * @param title2
	 */
	public void setTitle(final String title) {
		editTitle.setText(title);
		this.title.setText(title);
	}
}
